package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.InternalApi;

import java.util.List;


/**
 * 动态SQL 练习
 */
@Mapper
public interface UserInfo2Mapper {
    // 注解方式
    @Insert("<script>" +
            "insert into userinfo (" +
            "username," +
            "password," +
            "age," +
            "<if test='gender!= null'>gender,</if>" +
            "phone" +
            ") values (" +
            "#{username}," +
            "#{password}," +
            "#{age}," +
            "<if test='gender!= null'>#{gender},</if>" +
            "#{phone})" +
            "</script>")
    Integer insert(UserInfo userInfo);


    // 使用xml方式

    Integer insert2(UserInfo userInfo);

    // trim 的使用
    Integer insertByXML(UserInfo userInfo);


    List<UserInfo> selectByCondition(UserInfo userInfo);
    List<UserInfo> selectByCondition2(UserInfo userInfo);

    /**
     * 注解的方式实现
     * @param userInfo
     * @return
     */
    @Select("<script>" +
            "select * from userinfo" +
            "            <where>" +
            "                <trim prefixOverrides='and'>" +
            "                    <if test='username!=null'>username=#{username}</if>" +
            "                    <if test='age!=null'>and age=#{age}</if>" +
            "                    <if test='gender!=null'>and gender=#{gender}</if>" +
            "                </trim>" +
            "            </where>" +
            "</script>")
    List<UserInfo> selectByCondition3(UserInfo userInfo);
    Integer updateByCondition(UserInfo userInfo);
    Integer updateByCondition2(UserInfo userInfo);

    Integer batchDelete(List<Integer> ids);
}
